package com.css.project.process.definition.service.impl;

import java.util.List;

import com.css.common.utils.StringUtils;
import com.css.framework.web.page.PageDomain;
import com.css.framework.web.page.TableSupport;
import com.github.pagehelper.Page;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//import com.css.project.process.definition.mapper.ProcessDefinitionMapper;
import com.css.project.process.definition.service.IProcessDefinitionService;
import org.springframework.util.CollectionUtils;

/**
 * 流程定义Service业务层处理
 * 
 * @author dumk
 * @date 2020-04-22
 */
@Service
public class ProcessDefinitionServiceImpl implements IProcessDefinitionService 
{
    //@Autowired
    //private ProcessDefinitionMapper processDefinitionMapper;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    /**
     * 查询流程定义
     * 
     * @param id 流程定义ID
     * @return 流程定义
     */
    /*@Override
    public ProcessDefinition selectProcessDefinitionById(Long id)
    {
        return processDefinitionMapper.selectProcessDefinitionById(id);
    }*/

    /**
     * 查询流程定义列表
     * 
     * @param processDefinition 流程定义
     * @return 流程定义
     */
    @Override
    public Page<com.css.project.process.definition.domain.ProcessDefinition> selectProcessDefinitionList(com.css.project.process.definition.domain.ProcessDefinition processDefinition)
    {
        //return processDefinitionMapper.selectProcessDefinitionList(processDefinition);
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        Page<com.css.project.process.definition.domain.ProcessDefinition> list = new Page<>();
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        processDefinitionQuery.orderByProcessDefinitionId().orderByProcessDefinitionVersion().desc();
        if (StringUtils.isNotBlank(processDefinition.getName())) {
            processDefinitionQuery.processDefinitionNameLike("%" + processDefinition.getName() + "%");
        }
        if (StringUtils.isNotBlank(processDefinition.getKey())) {
            processDefinitionQuery.processDefinitionKeyLike("%" + processDefinition.getKey() + "%");
        }
        if (StringUtils.isNotBlank(processDefinition.getCategory())) {
            processDefinitionQuery.processDefinitionCategoryLike("%" + processDefinition.getCategory() + "%");
        }

        List<org.activiti.engine.repository.ProcessDefinition> processDefinitionList;
        if (pageNum != null && pageSize != null) {
            processDefinitionList = processDefinitionQuery.listPage((pageNum - 1) * pageSize, pageSize);
            list.setTotal(processDefinitionQuery.count());
            list.setPageNum(pageNum);
            list.setPageSize(pageSize);
        } else {
            processDefinitionList = processDefinitionQuery.list();
        }
        for (ProcessDefinition definition: processDefinitionList) {
            com.css.project.process.definition.domain.ProcessDefinition entity = new com.css.project.process.definition.domain.ProcessDefinition();
            entity.setId(definition.getId());
            entity.setKey(definition.getKey());
            entity.setName(definition.getName());
            entity.setCategory(definition.getCategory());
            entity.setVersion(definition.getVersion()+"");
            entity.setDescription(definition.getDescription());
            entity.setDeploymentId(definition.getDeploymentId());
            Deployment deployment = repositoryService.createDeploymentQuery()
                    .deploymentId(definition.getDeploymentId())
                    .singleResult();
            entity.setDeploymentTime(deployment.getDeploymentTime());
            list.add(entity);
        }
        return list;

    }

    /**
     * 批量删除流程定义
     *
     * @param ids 需要删除的流程定义ID
     * @return 结果
     */
    @Override
    public int deleteProcessDefinitionByIds(String[] ids)
    {
        int counter = 0;
        for (String deploymentId: ids) {
            List<ProcessInstance> instanceList = runtimeService.createProcessInstanceQuery()
                    .deploymentId(deploymentId)
                    .list();
            if (!CollectionUtils.isEmpty(instanceList)) continue;   // 跳过存在流程实例的流程定义
            repositoryService.deleteDeployment(deploymentId, true); // true 表示级联删除引用，比如 act_ru_execution 数据
            counter++;
        }
        return counter;
    }

   /* *//**
     * 新增流程定义
     * 
     * @param processDefinition 流程定义
     * @return 结果
     *//*
    @Override
    public int insertProcessDefinition(ProcessDefinition processDefinition)
    {
        return processDefinitionMapper.insertProcessDefinition(processDefinition);
    }

    *//**
     * 修改流程定义
     * 
     * @param processDefinition 流程定义
     * @return 结果
     *//*
    @Override
    public int updateProcessDefinition(ProcessDefinition processDefinition)
    {
        return processDefinitionMapper.updateProcessDefinition(processDefinition);
    }

    *//**
     * 批量删除流程定义
     * 
     * @param ids 需要删除的流程定义ID
     * @return 结果
     *//*
    @Override
    public int deleteProcessDefinitionByIds(Long[] ids)
    {
        return processDefinitionMapper.deleteProcessDefinitionByIds(ids);
    }

    *//**
     * 删除流程定义信息
     * 
     * @param id 流程定义ID
     * @return 结果
     *//*
    @Override
    public int deleteProcessDefinitionById(Long id)
    {
        return processDefinitionMapper.deleteProcessDefinitionById(id);
    }*/
}
